<?php
namespace app\admin\controller;

use think\Config;

class Excel extends Base{
	
	
	 public function exportExcel($data = array(), $title = array(), $filename = 'report')
    {	
	
		 	$title  = array(
		'账号序列',
		 '名字',
		'手机号',
		'尺寸',
		'收货地址',
		'领取时间'
		 );
		 $data = array();
		 $order=['create_time'=>'desc'];
         $select = M('content')->order($order)->select();
		 foreach($select as $k=> $st){
			 $data[$k]['id']=$st['id'];
			 $data[$k]['name']=$st['name'];
			 $data[$k]['keywords']=$st['keywords'];
			 $data[$k]['size']=$st['description'];
			 $add=explode("，",$st['content']);
			 $data[$k]['address']=$add[3];
		     $data[$k]['time']=date("Y-m-d h:i:sa", $st['create_time']);
		 }
		
		$filename="领取列表";
		
        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . $filename . ".xls");
        header("Pragma: no-cache");
        header("Expires: 0");

        //导出xls 开始
        if (!empty($title)) {
            foreach ($title as $k => $v) {
                $title[$k] = iconv("UTF-8", "GB2312", $v);
            }
            $title = implode("\t", $title);
            echo "$title\n";
        }

        if (!empty($data)) {
            foreach ($data as $key => $val) {
                foreach ($val as $ck => $cv) {
                    $data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
                }
                $data[$key] = implode("\t", $data[$key]);

            }
            echo implode("\n", $data);
        }
		//return $this->view->fetch();
    }
	
  	// public function  index(){
		// $this->display();
	// }
	// public function exportExcel($expTitle,$expCellName,$expTableData){
		// $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
		// $fileName = "免费申请列表";//or $xlsTitle 文件名称可根据自己情况设定
		// $cellNum = count($expCellName);
		// $dataNum = count($expTableData);
		
	
		// $objPHPExcel = new \org\PHPExcel\PHPExcel();
		
		
		// $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

		// $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
		// // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
		// for($i=0;$i<$cellNum;$i++){
			// $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
		// }
		// // Miscellaneous glyphs, UTF-8
		// for($i=0;$i<$dataNum;$i++){
			// for($j=0;$j<$cellNum;$j++){
				// $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
			// }
		// }

		// header('pragma:public');
		// header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
		// header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
		// $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		// $objWriter->save('php://output');
		// exit;
	// }
	// /**
	 // *
	 // * 导出Excel
	 // */
	// public function expUser(){//导出Excel
		// $xlsName  = "User";
		// $xlsCell  = array(
		// array('id','账号序列'),
		// array('name','名字'),
		// array('keywords','手机号'),
		// array('size','尺寸'),
		// array('address','收货地址'),
		// array('time','领取时间')
		// );
		
		// $order=['create_time'=>'desc'];
        // $select = M('content')->order($order)->select();
		
		// $xlsData=array();
		// foreach($select as $k=> $st){
			// $xlsData[$k]['id']=$st['id'];
			// $xlsData[$k]['name']=$st['name'];
			// $xlsData[$k]['keywords']=$st['keywords'];
			// $xlsData[$k]['size']=$st['description'];
			// $add=explode("，",$st['content']);
			// $xlsData[$k]['address']=$add[3];
			
		    // $xlsData[$k]['time']=date("Y-m-d h:i:sa", $st['create_time']);
		// }
		
		   // $this->exportExcel($xlsName,$xlsCell,$xlsData);
			
	// }

   
}
